﻿using System.IO;
using SDCloudSolution.SDCloud.BusinessLayer.Interfaces;
using SDCloudSolution.SDCloud.Entities.Models;

namespace SDCloudSolution.SDCloud.BusinessLayer
{
    public class ModelFileStorageProvider : IModelFileStorageProvider
    {
        private string _modelFolderStructureFormat =  "models" + Path.DirectorySeparatorChar + "{0}" + Path.DirectorySeparatorChar + "{1}";

        private readonly IFileStorageProvider _fileStorageProvider;

        public ModelFileStorageProvider(IFileStorageProvider fileStorageProvider)
        {
            _fileStorageProvider = fileStorageProvider;
        }

        public Stream GetModelFile(ModelEntity modelEntity)
        {
            var modelFilePath = GetModelFullFilePath(modelEntity);

            return _fileStorageProvider.GetFile(modelFilePath);
        }

        public void UploadModelFile(ModelEntity modelEntity, Stream inputStream)
        {
            var modelFilePath = GetModelFullFilePath(modelEntity);

            _fileStorageProvider.UploadFile(modelFilePath, inputStream);
        }

        public void DeleteModelFile(ModelEntity modelEntity)
        {
            var modelFilePath = GetModelFullFilePath(modelEntity);

            _fileStorageProvider.DeleteFile(modelFilePath);
        }

        public string GetModelFullFilePath(ModelEntity modelEntity)
        {
            return Path.Combine(
                _fileStorageProvider.GetFullFilePath(string.Format(_modelFolderStructureFormat
                , modelEntity.Id, modelEntity.FileName)));
        }
    }
}
